package com.sig.xml;

import org.xmlpull.v1.XmlPullParser;

import android.util.Log;

import com.sig.formes.Point;
import com.sig.map.*;

public class MapXmlParser extends XmlParser{

	public MapXmlParser(String xml) {
		super(xml);
	}
	public MapXmlParser(XmlPullParser xpp) {
		super(xpp);
	}

	public static String POLYGONE = "polygone";
	public static String CHEMIN = "chemin";
	public static String POINT = "point";	

	Map map = new Map();

	public Map getMap() {
		int eventType = XMLgetEventType(); 
		do { 
			if (eventType == XmlPullParser.START_TAG) {
				String tag = getXpp().getName();

				Log.d("XML","1");

				if(tag.equals(POLYGONE)){

					Polygone polygone = getPolygone();	
					Log.d("XML","Polygone: "+polygone.toString());
					map.addPolygone(polygone);

				}
				else if(tag.equals(CHEMIN)){
					Chemin chemin = getChemin(); 
					Log.d("XML","Chemin: "+chemin.toString());
					map.addChemin(chemin);
				}
			}
			eventType = XMLgetSuivant();
		} while (eventType != XmlPullParser.END_TAG && eventType != XmlPullParser.END_DOCUMENT);


		return map;
	}

	public Polygone getPolygone(){

		Polygone polygone = new Polygone();

		for(int i=0;i<getXpp().getAttributeCount();++i){
			String attrName = getXpp().getAttributeName(i);
			String attrValue = getXpp().getAttributeValue(i);

			//Log.d("XML","<Polygone: "+attrName+" = "+attrValue);

			if(attrName.equals("id")){
				Integer id = Integer.parseInt(attrValue);
				polygone.setId(id);
			}else if(attrName.equals("type")){
				String type = attrValue;
				polygone.setType(type);
			}
		}

		int eventType = XMLgetSuivant();
		do { 
			Log.d("XML","2");

			if (eventType == XmlPullParser.START_TAG) {
				String tag = getXpp().getName();

				if(tag.equals(POINT)){
					polygone.addPoint(getPoint());
				}else if(tag.equals(CHEMIN)){
					Chemin chemin = getChemin();
					polygone.addChemin(chemin);
				}

			}
			eventType = XMLgetSuivant();
		} while (eventType != XmlPullParser.END_TAG);

		return polygone;
	}

	public Chemin getChemin(){
		Chemin chemin = new Chemin();

		for(int i=0;i<getXpp().getAttributeCount();++i){
			String attrName = getXpp().getAttributeName(i);
			String attrValue = getXpp().getAttributeValue(i);

			//Log.d("XML","<Chemin: "+attrName+" = "+attrValue);

			if(attrName.equals("id")){
				Integer id = Integer.parseInt(attrValue);
				chemin.setId(id);
			}
		}

		int eventType = XMLgetSuivant();

		while(eventType != XmlPullParser.END_TAG){
			Log.d("XML","3");

			if (eventType == XmlPullParser.START_TAG) {
				String tag = getXpp().getName();

				if(tag.equals(POINT)){
					chemin.addPoint(getPoint());
				}
				if(tag.equals(POLYGONE)){
					Polygone polygone = getPolygone();
					chemin.addPolygone(polygone);
				}
				else if(tag.equals(CHEMIN)){
					Chemin chemin2 = getChemin();
					chemin.addChemin(chemin2);
				}
			}

			eventType = XMLgetSuivant();
		} 


		return chemin;
	}

	public static String POINT_X = "x";
	public static String POINT_Y = "y";

	public Point getPoint(){
		Point point = new Point();

		int eventType = XMLgetEventType(); 
		do { 
			if (eventType == XmlPullParser.START_TAG) {
				Float x = Float.parseFloat(getXpp().getAttributeValue(null,POINT_X));
				Float y = Float.parseFloat(getXpp().getAttributeValue(null,POINT_Y));

				point.setX(x);
				point.setY(y);
			}
			eventType = XMLgetSuivant();
		} while (eventType != XmlPullParser.END_TAG);


		return point;
	}

}